package com.cskaoyan.market.admin.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cskaoyan.market.admin.service.AdminStatService;
import com.cskaoyan.market.admin.service.impl.AdminStatServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

/**
 * 统计报表控制层
 *
 * @author BlackFlame33
 * @date 2023/03/22
 **/
@WebServlet("/admin/stat/*")
public class AdminStatController extends HttpServlet {
    private AdminStatService adminStatService = new AdminStatServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/stat", "");
        switch (op) {
            case "/user":
                getUserStat(req, resp);
                break;
            case "/order":
                getOrderStat(req, resp);
                break;
            case "/goods":
                getGoodStat(req, resp);
                break;
            default:
                resp.setStatus(404);
        }
    }

    private void getGoodStat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> data = adminStatService.getGoodStat();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));

    }

    private void getUserStat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> data = adminStatService.getUserStat();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }

    private void getOrderStat(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> data = adminStatService.getOrderStat();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(data)));
    }
}